package com.zhou.org.interceptor;

import com.zhou.framework.exception.AuthException;
import com.zhou.framework.jwt.annotation.PassToken;
import com.zhou.org.common.MenuCache;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

/**
 * @author lang.zhou
 * @date 2022/6/21 15:51
 */
public class RightInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse httpServletResponse, Object object){
        if(!(object instanceof HandlerMethod)){
            return true;
        }
        /*String uri = request.getRequestURI();
        if(uri.endsWith("/error/400") || uri.endsWith("/error/401") || uri.endsWith("/error/404")  || uri.endsWith("/error/500") ){
            return true;
        }*/
        HandlerMethod handlerMethod=(HandlerMethod)object;
        Method method=handlerMethod.getMethod();
        if (method.isAnnotationPresent(PassToken.class)) {
            return true;
        }
        if(!MenuCache.checkRequest(request)){
            throw new AuthException("权限不足");
        }
        return true;
    }
    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView){
    }
    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e){
    }
}
